package com.yifeng.repo.flink.data.transport.dto;

import java.beans.Transient;
import java.util.List;

public class DataPacket {
    // 当前最后一条数据在全量数据中的位置, 第一条从1开始, 负数指已读完毕, 且其绝对值为原表读取数据总量.
    private long index;
    
    private int queueNo;

    private List<DataRow> rows;

    //用户名
    private String schema;
    
    // 表名
    private String tableName;
    
    // 库名
    private String databaseName;
    
    //主机名
    private String host;

    private boolean end = false;

    protected DataPacket() {
    }

    public static DataPacket ofFull(int queueNo, long idx, List<DataRow> rows, String tableName,String databaseName, String schema,String host) {
        return new DataPacket(queueNo,idx, rows, System.currentTimeMillis(),tableName,databaseName,schema,host);
    }

    public static DataPacket ofFull(int queueNo,long idx , List<DataRow> rows, String tableName ,String databaseName,String schema,String host, boolean end) {
        DataPacket dataPacket = new DataPacket(queueNo,idx, rows, System.currentTimeMillis(),tableName,databaseName,schema,host);
        dataPacket.setEnd(end);
        return dataPacket;
    }

    private DataPacket(int queueNo,long index , List<DataRow> rows, long time, String tableName,String databaseName, String schema,String host) {
        this.queueNo = queueNo;
    	this.index = index;
        this.rows = rows;
        this.tableName = tableName;
        this.databaseName = databaseName;
        this.host = host;
        this.schema = schema;
        // 给每行数据赋值时间戳
        if(rows != null){
            rows.forEach(o -> {
                o.setTs_ms(time);
            });
        }
    }

    public boolean isEnd() {
        return end;
    }

    public void setEnd(boolean end) {
        this.end = end;
    }

    /**
     * 是否为全量读开始标记数据包, 此时仅index有有效值
     *
     * @return 是否为全量读开始标记数据包
     */
    @Transient
    public boolean isFullStart() {
        return index == 0;
    }

    /**
     * 是否为全量读结束标记数据包, 此时仅index有有效值
     *
     * @return 是否为全量读结束标记数据包
     */
    @Transient
    public boolean isFullEnd() {
        return index < 0;
    }

    public long getIndex() {
        return index;
    }

    public void setIndex(long index) {
        this.index = index;
    }

    public List<DataRow> getRows() {
        return rows;
    }

    public void setRows(List<DataRow> rows) {
        this.rows = rows;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    
    public String getDatabaseName() {
		return databaseName;
	}

	public void setDatabaseName(String databaseName) {
		this.databaseName = databaseName;
	}
	
	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}
	
	public int getQueueNo() {
		return queueNo;
	}

	public void setQueueNo(int queueNo) {
		this.queueNo = queueNo;
	}

	@Override
    public String toString() {
        return "DataPacket{" +
        		"queueNo=" + queueNo +
                "index=" + index +
                ", rows=" + rows +
                ", host=" + host +
                ", schema=" + schema +
                ", tableName='" + tableName + '\'' +
                ", databaseName='" + databaseName + '\'' +
                ", end=" + end +
                '}';
    }
}
